#include <string>
#include <iostream>

#include "boost/regex.hpp"
#include "gtest/gtest.h"

using namespace std;
using namespace boost;

TEST(RegexTest, Usage)
{
  //regex url_regex("(http)://((.+?)(:(.+?))?@)?(.+?)(:(.+?))?(/.+?)?(\\?(.+?)?)?(#(.+)?)?");
  //regex url_regex("(co|com|edu|gov|net|org)\\.");
  //regex url_regex("(/(.*/)?)(.*?(\\.(.*))?)?");
  regex url_regex(".*?<a .*href=(\"|')(.+?)(\"|').*>(.*?)?</a>(.*?)");
  cout << "url_regex:" << url_regex << endl;
  smatch what;
  //string url = "http://user:pwd@example.com:80/a/c/index.php?a=3&b=4#ref";
  string url = "abc\n<a id=\"3\" \nhref=\"/index.html\" class=\"3\">haha</a>abc";
  //string url = "/a/c/";

  bool matched = regex_match(url, what, url_regex);
  cout << "url_regex:" << url_regex << endl;
  if (!matched) {
    cout << "not match:" << url << endl;
    return;
  }
  for (size_t i=0; i<what.size(); ++i) {
    cout << i << ":" << what[i].matched << endl;
    if (what[i].matched) {
      cout << i << ":" << std::string(what[i].first, what[i].second) << endl;
      cout << i << ":" << what[i].second - url.begin() << endl;
    } else {
      cout << i << "s - f:" << what[i].first - what[i].second << endl;
    }
  }
}

